// Copyright (C) 2016  Francisco Javier Parra <franparpe@openmailbox.org>

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#include "main-window.h"
#include "conversation_container.h"
#include "widget_roster_panel.h"
#include <iostream>

// static
// void si_button_clicked(ustring &message)
// {
//   std::cout << message << "\n";
// }

MainWindow::MainWindow()
  : Gtk::ApplicationWindow(),
  m_button("Send"),
  m_button2("A ver si esto va"),
  m_rosterpanel_reveal_btn("go-next"),
  m_test_hide_btn("hiding things"), // Remove later
  m_vbox(Gtk::ORIENTATION_VERTICAL, 0)
{
  generateUI();
  // Signals
  m_button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::sendMessage));
  m_button2.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::onConnectSignal));
  m_rosterpanel_reveal_btn.signal_clicked().connect(sigc::mem_fun(*this,
                                                                  &MainWindow::showHideSideBar));
  show_all_children();
  std::cout << "Window created" << "\n";

  // dispatcher connection
  m_msg_dispatcher.connect(sigc::mem_fun(*this, &MainWindow::on_message_from_gloox));

}

void MainWindow::generateUI()
{
  m_headerbar.set_show_close_button();
  set_titlebar(m_headerbar);
  set_title("Hello mrtx");

  set_default_size(400, 400);

  // Adding the main container in the window
  add(m_overlay);

  // Aligning the buttons
  m_button.set_halign(Gtk::ALIGN_CENTER);
  m_button.set_valign(Gtk::ALIGN_CENTER);

  // Placing widgets
  // m_main_hbox.pack_start(m_sidebar);
  // m_vbox.add(m_overlay);
  m_overlay.add(m_vbox);
  m_overlay.add_overlay(m_sidebar);
  m_sidebar.set_transition_type(Gtk::RevealerTransitionType::REVEALER_TRANSITION_TYPE_SLIDE_LEFT);
  m_sidebar.set_reveal_child(true);
  // m_main_hbox.pack_start(m_vbox, Gtk::PACK_EXPAND_WIDGET);
  m_vbox.pack_start(m_conversationCont, Gtk::PACK_EXPAND_WIDGET);
  m_vbox.pack_start(m_hbox, Gtk::PACK_SHRINK);
  m_hbox.pack_start(m_entry, Gtk::PACK_EXPAND_WIDGET);
  m_hbox.pack_start(m_button, Gtk::PACK_SHRINK);
  m_hbox.pack_start(m_button2, Gtk::PACK_SHRINK);
  // GtkHeaderBar
  m_headerbar.pack_start(m_rosterpanel_reveal_btn);
  m_headerbar.pack_start(m_test_hide_btn);
}

void MainWindow::sendMessage()
{
  m_text_msg = m_entry.get_text();
  std::cout << m_text_msg << "\n";
  m_conversationCont.addMessage(m_text_msg);
  // m_xmpplar_client->
}

void MainWindow::receiveMessage(const gloox::Message& stanza)
{
  // m_text_msg_trd = stanza.from();
  m_conversationCont.addMessage(stanza.body());
}


void MainWindow::onConnectSignal()
{
//   gloox::JID jid("");
  
//   m_xmpplar_client = new XmpplarClient(this, jid, "");
//   m_conn_thread = new std::thread([this](){m_xmpplar_client->startConnection();});
}

 void MainWindow::message_notification()
 {
   m_msg_dispatcher.emit();
 }

 void MainWindow::on_message_from_gloox()
 {
   m_conversationCont.addMessage("Message receved");
   m_conversationCont.addMessage(m_xmpplar_client->getBody());
 }

void MainWindow::showHideSideBar()
{
  if (m_sidebar.get_child_revealed()) {
    m_sidebar.set_reveal_child(false);
  }else{
    m_sidebar.set_reveal_child(true);
  }
}
